<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
        </div>
      </template>

      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="新用户名" prop="username">
          <el-input 
            v-model="form.username" 
            placeholder="不修改请留空"
          />
        </el-form-item>

        <el-form-item label="原密码" prop="oldPassword">
          <el-input
            v-model="form.oldPassword"
            type="password"
            placeholder="修改密码时必填"
            show-password
          />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="form.newPassword"
            type="password"
            placeholder="修改密码时必填"
            show-password
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            placeholder="修改密码时必填"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSubmit">
            保存修改
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { userApi } from '@/api/user'

const form = ref({
  username: '',
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 修改表单验证规则
const rules = {
  username: [
    { required: false, message: '请输入新用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  oldPassword: [
    { required: false, message: '请输入原密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: false, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: false, message: '请再次输入新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (form.value.newPassword && value !== form.value.newPassword) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 修改提交逻辑
const handleSubmit = async () => {
  try {
    // 检查是否有任何字段被修改
    if (!form.value.username && !form.value.oldPassword) {
      ElMessage.warning('请至少修改一项内容')
      return
    }

    // 如果要修改密码，需要验证所有密码字段
    if (form.value.oldPassword || form.value.newPassword || form.value.confirmPassword) {
      if (!form.value.oldPassword || !form.value.newPassword || !form.value.confirmPassword) {
        ElMessage.warning('修改密码时需要填写所有密码字段')
        return
      }
      if (form.value.newPassword !== form.value.confirmPassword) {
        ElMessage.error('两次输入的新密码不一致')
        return
      }
    }

    const updateData = {}
    
    // 只包含已修改的字段
    if (form.value.username) {
      updateData.username = form.value.username
    }
    if (form.value.oldPassword && form.value.newPassword) {
      updateData.old_password = form.value.oldPassword
      updateData.new_password = form.value.newPassword
    }

    await userApi.updateProfile(updateData)
    ElMessage.success('修改成功')
    
    // 重置表单
    form.value = {
      username: '',
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    }
  } catch (error) {
    console.error('修改失败:', error)
    ElMessage.error(error.response?.data?.error || '修改失败')
  }
}
</script>

<style scoped>
.profile-container {
  padding: 20px;
}

.profile-card {
  max-width: 600px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
